package com.alex.xmlyfm.services;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import com.alex.xmlyfm.Constants;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by
 * Author: alex
 * Email:
 * Date: 15-10-24.
 */
public class MusicService extends Service {

    private MediaPlayer mediaPlayer;
    private ArrayList<String> musicList;
    private int currentMusic = 0;

    private static Handler handler;
    private boolean threadFlag;




    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
        musicList = new ArrayList<>();
        threadFlag = true;

        new Thread(){
            @Override
            public void run() {
                super.run();
                while(threadFlag){
                    Message msg = Message.obtain();
                    msg.arg1 = currentMusic;
                    handler.sendMessage(msg);
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            musicList.clear();
            musicList.addAll(intent.getStringArrayListExtra(Constants.ACTION_MUSIC_PLAY));
            if (mediaPlayer.isPlaying()){
                mediaPlayer.stop();

            }

            currentMusic = intent.getIntExtra(Constants.ACTION_MUSIC_PLAY_CURRENT, 0);
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(musicList.get(currentMusic++));
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.prepareAsync();

            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {

                    mediaPlayer.start();
                }
            });

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    try {
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource(musicList.get(currentMusic++));
                        mediaPlayer.prepareAsync();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        final MusicBinder musicBinder = new MusicBinder();
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                musicBinder.isPlay();
            }
        };

        return musicBinder;
    }

    public class MusicBinder extends Binder{

        public boolean isPlay(){
            boolean ret = false;
            ret = mediaPlayer.isPlaying();
            return ret;
        }

        public int getPosition(){
            int ret = 0;
            ret = currentMusic;

            return ret;
        }

    }

    class ProgressThread extends Thread{
        @Override
        public void run() {
            super.run();
        }
    }

}
